/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.worsik.vypocty.topeni.tepelneztraty.dao;

import cz.worsik.vypocty.topeni.tepelneztraty.model.Obecny;
import static cz.worsik.vypocty.topeni.tepelneztraty.dao.SqlStatements.SELECT_LAST_ID;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

/**
 *
 * @author 231260
 */
public abstract class ObecnyDao {

    private static final Logger log = LoggerFactory.getLogger(ObecnyDao.class);

    /** "ORM" integrace (Spring JdbcTemplate) */
    protected NamedParameterJdbcTemplate jt;

    protected long insert(String sql, Obecny objekt) {
		
		long id = findIdForInsert();
		objekt.setId(id);
		
		log.debug("Vkladam objekt "+objekt.getClass().getSimpleName());
		
        jt.update(sql, new BeanPropertySqlParameterSource(objekt));
		
        return id;
    }

    protected void update(String sql, Obecny objekt) {
        jt.update(sql, new BeanPropertySqlParameterSource(objekt));
    }

    public void setJt(NamedParameterJdbcTemplate jt) {
        this.jt = jt;
    }
	
	protected long findIdForInsert()
	{
        long id = jt.queryForLong(SELECT_LAST_ID+getTable(),new HashMap()) + 1;
		
		return id;
	}

	public abstract String getTable();
}